// pages/frontFinance/frontFinance.js
var api = require('../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeArr: ['学费', '服装', '比赛', '活动','其他'],
    budgetArr:['收入','支出'],
    projectArr:[],
    budgetIdx:'',
    projectIdx:'',
    typeIdx:'',
    date1:'',
    date2:'',
    financeList:[]
  },

  //获取校区、项目、班级
  getSchool() {
    api.fetchApi('reception/getsearch', {
      token: wx.getStorageSync('token')
    }).then(res => {
      var classArr = res.class;
      var projectArr = res.project;
      var campusArr = res.campus;
      campusArr.push({ id: '', name: "其他" })
      //console.log(campusArr, classArr, projectArr)
      this.setData({
        projectArr: campusArr,
      })

    })
  },

  //获取财务列表
  getFinanceList(page){
    var params={
      token: wx.getStorageSync('token'),
      page,
      type: this.data.typeIdx,
      budget: this.data.budgetIdx,
      project_id: this.data.project_id,
      time:this.data.date1+"~"+this.data.date2
    }
    api.fetchApi('reception/finance_index', params).then(res=>{
      var financeList = this.data.financeList,
          financeList1 = res;
      if (financeList1.length == 0 && page > 1) {
       // api.showToast('到底啦!', 'http://qnwmfx.laigl.com/iconimg/warn.png', 1500)
        this.setData({
          isMore: true
        })
        return
      }
      financeList = [...financeList, ...financeList1]
      this.setData({
        financeList,
        page
      })
    })
  },

//新增财务
  addFinance(){
    wx.navigateTo({
      url: '../addFinance/addFinance',
    })
  },

  //选择类型
  selectType(e){
    var idx=e.detail.value;
    this.setData({
      typeIdx:idx
    })
    this.setData({
      financeList:[]
    })
    this.getFinanceList(1)

  },

  //类型收支
  selectBudget(e){
    var idx = e.detail.value;
    this.setData({
      budgetIdx: idx
    })
    this.setData({
      financeList: []
    })
    this.getFinanceList(1)
  },

  //选择项目
  selectProject(e){
    var idx = e.detail.value;
    var nowArr = this.data.projectArr[idx];
    var project_id=nowArr.name
    this.setData({
      projectIdx: idx,
      project_id
    })
    this.setData({
      financeList: []
    })
    this.getFinanceList(1)
  },

  dateChange1(e){
    this.setData({
      date1:e.detail.value
    })
    this.setData({
      financeList: []
    })
    this.getFinanceList(1)
  },
  dateChange2(e) {
    this.setData({
      date2: e.detail.value
    })
    this.setData({
      financeList: []
    })
    this.getFinanceList(1)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    var date1 = new Date().dateFormat('Y-m-d');
    var date2 = (new Date(new Date().valueOf() + 86400000)).dateFormat('Y-m-d')
    console.log(date1, date2)
    this.setData({
      date1,
      date2
    })

    this.getSchool();
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getFinanceList(1)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      financeList: []
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var page = this.data.page || 1;
    page++;

    this.getFinanceList(page)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})